<?php
namespace app\manage\controller;
use app\manage\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Cache;
use app\model\AuthGroup;
use app\model\Admin as Adminmodel;

class Admin extends BaseController
{
    public function index()
    {
        $key=input('key','');
        $data=Adminmodel::alias('a')
        ->join('auth_group w','a.role = w.id', 'LEFT')
        ->where('username','like','%'.$key.'%')
        ->order('a.id','desc')->field( 'a.id,username,w.title,a.create_time,a.update_time,a.desc,a.isopen' )->paginate(10,false,['query'=>request()->param()]);
        View::assign('data',$data);
        View::assign('count1', $data->total());
    	return View::fetch();
    }
	public function ajax()
    {
    	$data=input('post.');
		if($data['type']=='admin_start'){
			if(Adminmodel::update(['isopen' => 1], ['id' => $data['id']])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
       	if($data['type']=='admin_stop' and $data['id'] != 1){
			if(Adminmodel::update(['isopen' => 0], ['id' => $data['id']])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='admin_del' and $data['id'] != 1){
			if(Adminmodel::destroy($data['id'])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='admin_all'){
			if(in_array("1", $data['id'])){
                return json(['code' => 0, 'message' => '操作失败']);
			}
			if(Adminmodel::destroy($data['id'])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
        return json(['code' => 0, 'message' => '失败']);
    }
    public function add()
    {
        $admin=new Adminmodel();
        if(request()->isPost()){
            $data=input('post.');
            $validate = new \app\manage\validate\Admin;
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $shield=Db::name('shield')->where('id',1)->value('shield');
            if(\in_array($data['username'],explode("|",$shield))){
                $this->error('用户名非法');
            }
            $info=$admin->add($data);
            if($info == 1){
                $this->success('添加成功',url('admin/index',['st'=>1]));
            }else{
                $this->error('添加失败了');
            }
        }
        $res=AuthGroup::where('isopen',1)->select();
        View::assign('res',$res);
        return View::fetch();
    }
	public function edit()
    {
    	if(request()->isPost()){
    		$data=input('post.');
			$validate = new \app\manage\validate\Admin;
			if(!$validate->scene('edit')->check($data)){
				$this->error($validate->getError());
			}
			if($data['password']!=''){
				$data['password']=md5(substr(md5($data['password']),0,25).'lizhili');
			}else{
				unset($data['password']);
			}
			if(Adminmodel::update($data, ['id'=>$data['id']])){
				$this->success('修改成功',url('admin/index',['st'=>1]));
			}else{
				$this->error('修改失败了');
			}
    	}
		$cid=input('id',0);
		$data=Adminmodel::where('id',$cid)->field('id,username,desc,role')->find();
        View::assign('data',$data);
		$res=AuthGroup::where('isopen',1)->select();
        View::assign('res',$res);
       return View::fetch();
    }
	//清除缓存
	public function cahe(){
		if(Cache::clear()){
			$this->success('清除缓存成功',url('index/main'));
		}else{
			$this->error('清除缓存失败了');
		}
	}
}
